#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h> 
#include <string.h>

#include <stdio.h>
class tcpclient
{
public:
	tcpclient(std::string server_ip, int server_port)
		: _sock(-1)
		, _server_ip(server_ip)
		, _server_port(server_port)
	{}

	void InitClient()
	{
		//创建套接字
		_sock = socket(AF_INET, SOCK_STREAM, 0);
		if (_sock < 0)
		{
			std::cerr << "socket error" << std::endl;
			exit(2);
		}
	}

	~tcpclient()
	{
		if (_sock >= 0)
		{
			close(_sock);
		}
	}

	void Request()
	{
		std::string msg;
		char buff[1024];
		for(;;)
		{
			std::cout << "Please Enter# " << std::endl;
			getline(std::cin, msg);

			write(_sock, msg.c_str(), msg.size());

			ssize_t Size = read(_sock, buff, sizeof(buff)-1);
			if(Size > 0)
			{
				buff[Size] = '\0';
				std::cout << "server echo# " << buff << std::endl;
			}
			else if(Size == 0)
			{
				std::cout << "server close!" << std::endl;
				break;
			}
			else
			{
				std::cerr << "read error!" << std::endl;
				break;
			}
		}
	}

	void Start()
	{
		struct sockaddr_in peer;
		memset(&peer, '\0', sizeof(peer));
		peer.sin_family = AF_INET;
		peer.sin_port = htons(_server_port);
		peer.sin_addr.s_addr = inet_addr(_server_ip.c_str());

		if(connect(_sock, (sockaddr*)&peer, sizeof(peer)) == 0)
		{
			std::cout << "connect success!" << std::endl;
			Request();
		}
		else
		{
			std::cerr << "connect error!" << std::endl;
			return;
		}
	}


private:
	int _sock; //套接字
	std::string _server_ip; //服务端IP地址
	int _server_port; //服务端端口号
};

void Usage(std::string proc)
{
	std::cout << "Usage: " << proc << "server_ip server_port" << std::endl;
}

int main(int argc, char* argv[])
{
	if (argc != 3)
	{
		Usage(argv[0]);
		exit(1);
	}
	std::string server_ip = argv[1];
	int server_port = atoi(argv[2]);
	tcpclient* clt = new tcpclient(server_ip, server_port);
	clt->InitClient();
	clt->Start();
	return 0;
}
